﻿using COUP.Core.Enums;

namespace COUP.Core.Networking
{
    public unsafe class ChatPacket : Packet
    {
        public ChatPacket(Packet Data)
        {
            buffer = Data.Buffer;
        }

        public ChatPacket(string To, string From, string Message, int UniqueID, ChatType Style)
        {
            Length = (ushort)(21 + To.Length + From.Length + Message.Length);
            Type = 1004;
            Int((uint)ChatColour.Black);
            Short((ushort)Style);
            Int(UniqueID, 12);
            Byte(4);
            StringWithLength(From);
            StringWithLength(To);
            StringWithLength("");
            StringWithLength(Message);
        }

        public ChatType MessageType
        {
            get
            {
                fixed (byte* lpbuffer = buffer)
                    return (ChatType)(*(ushort*)(lpbuffer + 8));
            }
        }

        public string From
        {
            get
            {
                fixed (byte* lpBuffer = buffer)
                    return new string((sbyte*)lpBuffer, 18, (byte)*(byte*)(lpBuffer + 17)).TrimEnd('\0');
            }
        }

        public string To
        {
            get
            {
                fixed (byte* lpBuffer = buffer)
                    return new string((sbyte*)lpBuffer, 19 + From.Length, (byte)*(byte*)(lpBuffer + 18 + From.Length)).TrimEnd('\0');
            }
        }


        public string Message
        {
            get
            {
                fixed (byte* lpBuffer = buffer)
                    return new string((sbyte*)lpBuffer, 21 + From.Length + To.Length, (byte)*(byte*)(lpBuffer + 20 + From.Length + To.Length)).TrimEnd('\0');
            }
        }
    }
}
